#!/bin/bash
# 词典单词数据库存储 - 快速部署脚本
# 使用方法: ./dict_import_setup.sh

set -e

echo "=========================================="
echo "词典单词数据库存储 - 快速部署"
echo "=========================================="
echo ""

# 配置
DB_CONTAINER="ruoyi-mysql"
DB_USER="root"
DB_PASS="123"
DB_NAME="ry-vue"
DICT_PATH="/root/workspace/3.1.0-dev/work/aiword/public/dicts"

# 检查数据库容器
echo "[1/5] 检查数据库容器..."
if ! docker ps | grep -q $DB_CONTAINER; then
    echo "❌ 数据库容器未运行: $DB_CONTAINER"
    exit 1
fi
echo "✅ 数据库容器正常"
echo ""

# 执行SQL脚本
echo "[2/5] 执行数据库脚本..."
if [ -f "ruoyi-module-demo/sql/dict_word_table.sql" ]; then
    docker exec -i $DB_CONTAINER mysql -u$DB_USER -p$DB_PASS \
        --default-character-set=utf8mb4 $DB_NAME \
        < ruoyi-module-demo/sql/dict_word_table.sql
    echo "✅ 数据库脚本执行成功"
else
    echo "❌ SQL脚本文件不存在"
    exit 1
fi
echo ""

# 验证表结构
echo "[3/5] 验证表结构..."
TABLE_COUNT=$(docker exec -i $DB_CONTAINER mysql -u$DB_USER -p$DB_PASS \
    --default-character-set=utf8mb4 $DB_NAME \
    -se "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='$DB_NAME' AND table_name IN ('dict_word', 'dict_word_learning')")

if [ "$TABLE_COUNT" -eq 2 ]; then
    echo "✅ 表结构创建成功（dict_word, dict_word_learning）"
else
    echo "⚠️  警告: 只创建了 $TABLE_COUNT 个表"
fi

# 检查dict_book表的新字段
echo "   检查dict_book表的新字段..."
FIELD_COUNT=$(docker exec -i $DB_CONTAINER mysql -u$DB_USER -p$DB_PASS \
    --default-character-set=utf8mb4 $DB_NAME \
    -se "SELECT COUNT(*) FROM information_schema.columns WHERE table_schema='$DB_NAME' AND table_name='dict_book' AND column_name IN ('source_type', 'user_id', 'is_loaded')")

if [ "$FIELD_COUNT" -eq 3 ]; then
    echo "✅ dict_book表字段更新成功"
else
    echo "⚠️  警告: dict_book表只添加了 $FIELD_COUNT 个新字段"
fi
echo ""

# 重新编译和部署
echo "[4/5] 重新编译和部署应用..."
read -p "是否重新编译部署应用？(y/n): " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
    if [ -f "build.sh" ]; then
        bash build.sh
        echo "✅ 应用重新部署成功"
    else
        echo "⚠️  找不到build.sh，请手动编译部署"
    fi
else
    echo "⚠️  跳过应用重新部署"
fi
echo ""

# 显示统计信息
echo "[5/5] 显示统计信息..."
echo "-------------------"
echo "词典总数:"
docker exec -i $DB_CONTAINER mysql -u$DB_USER -p$DB_PASS \
    --default-character-set=utf8mb4 $DB_NAME \
    -se "SELECT COUNT(*) FROM dict_book"

echo ""
echo "已导入单词的词典:"
docker exec -i $DB_CONTAINER mysql -u$DB_USER -p$DB_PASS \
    --default-character-set=utf8mb4 $DB_NAME \
    -se "SELECT book_id, COUNT(*) as word_count FROM dict_word GROUP BY book_id"

echo ""
echo "词典文件目录:"
if [ -d "$DICT_PATH" ]; then
    FILE_COUNT=$(ls -1 $DICT_PATH/*.json 2>/dev/null | wc -l)
    echo "  路径: $DICT_PATH"
    echo "  文件数: $FILE_COUNT 个JSON文件"
else
    echo "  ⚠️  词典目录不存在: $DICT_PATH"
fi
echo ""

echo "=========================================="
echo "✅ 部署完成！"
echo "=========================================="
echo ""
echo "📖 下一步操作："
echo ""
echo "1. 查看完整文档:"
echo "   cat DICT_WORD_STORAGE_GUIDE.md"
echo ""
echo "2. 测试导入单个词典（需要登录获取token）:"
echo "   curl -X POST http://localhost:8080/module/dict/word/import/cet4 \\"
echo "     -H \"Authorization: Bearer YOUR_TOKEN\""
echo ""
echo "3. 批量导入所有词典:"
echo "   curl -X POST http://localhost:8080/module/dict/word/import/batch \\"
echo "     -H \"Authorization: Bearer YOUR_TOKEN\""
echo ""
echo "4. 查看服务日志:"
echo "   tail -f logs/ruoyi-admin.jar.log"
echo ""
echo "5. 检查导入结果:"
echo "   docker exec -i $DB_CONTAINER mysql -u$DB_USER -p$DB_PASS \\"
echo "     --default-character-set=utf8mb4 $DB_NAME \\"
echo "     -e \"SELECT book_id, COUNT(*) FROM dict_word GROUP BY book_id;\""
echo ""


